<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Goods;

class GoodsController extends Controller
{
    //
    public function getGoods(Request $request){
        $goods = Goods::orderBy('id')-> get();
        return response([
            'data' => $goods,
            'code' => 200
        ]);
    }

    public function addGoods(Request $request){
//        $new = News::where('type',$request->get('type'));
        $data = $request->data;
//        dd($data['add']);
        foreach($data['add']??[] as $key => $item){
            Goods::create([
                'good_name' => $item['good_name'],
                'total' => $item['total'],
                'start_port' => $item['start_port'],
                'end_port' => $item['end_port'],
                'laycan' => $item['laycan'],
            ]);
        }
        Goods::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Goods::find($item['id'])->update([
                'good_name' => $item['good_name'],
                'total' => $item['total'],
                'start_port' => $item['start_port'],
                'end_port' => $item['end_port'],
                'laycan' => $item['laycan'],
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
